package com.xinkao.log.config.base;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.xinkao.log.util.RedisUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.UnauthenticatedException;

import javax.annotation.Resource;

/**
 * Controller公共组件
 *
 * @author lililiang
 */
public abstract class BaseController {
    @Resource
    private RedisUtils redisUtils;

    public JSONObject getUser(){
        try {
            return redisUtils.get(SecurityUtils.getSubject().getPrincipal().toString(), JSONObject.class);
        }catch(Exception e){
            throw new UnauthenticatedException();
        }
    }

    public String getUserId(){
        try {
            JSONObject user = this.getUser();
            if(ObjectUtil.isNotEmpty(user)){
                return user.getString("userId");
            }else{
                return null;
            }
        }catch(Exception e){
            throw new UnauthenticatedException();
        }
    }


}
